/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.marist.cs.os.g3.mos;

/**
 *
 * @author Intern
 */
public class Block {

    Word[] data;
    boolean empty;

    public Block() {
        data = new Word[10];
        empty = true;
        for (int i = 0; i < data.length; i++) {
            data[i] = new Word();
        }
    }

    public Block(Word[] data) {
        this.data = new Word[10];
        System.arraycopy(data, 0, this.data, 0, this.data.length);
        empty = false;
    }

    public Block(String string) {
        //The string must be of length 10*4 = 40.
        data = new Word[10];
        empty = true;
        setData(string);
    }

    @Override
    public String toString() {
        StringBuilder buff = new StringBuilder();
        for (Word word : data) {
            if (word.toString() != null) {
                buff.append(word.toString());
            }
        }
        return buff.toString();
    }

    public boolean isEmpty() {
        
       
        return empty;
    }

    public void setEmpty() {
        empty = true;
    }

    public boolean setData(String string) {
        
        
        string = padTo40(string);
        for (int i = 0, begin = 0, end = 4; end <= 40 && i < 10; begin += 4, end += 4, i++) {
            if (end > string.length()) {
                data[i] = new Word();
            } else {
                data[i] = new Word(string.substring(begin, end));
            }
        }
        empty = false;
        return true;
    }

    public Word getWord(int i) {
        return data[i];
    }

    public Word[] getBlock() {
        return data;
    }

    private String padTo40(String string) {
        for (int i = 0; i < 40 - string.length(); i++) {
            string += " ";
        }
        return string;
    }
    
    public void setEmpty(boolean empty){
        this.empty = empty;
    }
}
